package com.jsshse.service.demand.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import com.jsshse.service.base.impl.BaseServiceImpl;
import com.jsshse.dao.demand.UpDetailDaoI;
import com.jsshse.dao.demand.UpLogDaoI;
import com.jsshse.service.demand.UpDetailServiceI;
import com.jsshse.util.MD5;
import com.jsshse.util.Pages;
import com.jsshse.util.CommUtil;

import org.apache.log4j.Logger;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service("upDetailService")
public class UpDetailServiceImpl extends BaseServiceImpl implements UpDetailServiceI{
	private static Logger log = Logger.getLogger(UpDetailServiceImpl.class);
	@Resource
	private UpDetailDaoI upDetailDao ;
	@Resource
	private UpLogDaoI upLogDao;
	
	@Override
	@CacheEvict(value = "d_up_detail", allEntries = true)
	public boolean add(Map map) {
		String id=CommUtil.getId();
		map.put("id", id);
		String dt_num="";
		
		List list=upDetailDao.queryForListByPage(" and log_id='"+map.get("log_id").toString()+"' order by dt_num desc ",0,1);
		if(list.size()==0){
			dt_num="001";
		}else{
			Map upDetail=(Map)list.get(0);
			int numb=Integer.parseInt(upDetail.get("dt_num").toString().substring(9, 12))+1;
			if(String.valueOf(numb).length()==1){
				dt_num="00"+numb;
			}else if(String.valueOf(numb).length()==2){
				dt_num="0"+numb;
			}else{
				dt_num=String.valueOf(numb);
			}
		}
		Map upLog=upLogDao.queryById(map.get("log_id").toString());
		dt_num=upLog.get("up_num").toString()+dt_num;
		
		map.put("dt_num", dt_num);
		return upDetailDao.add(map);
	}

	@Override
	@CacheEvict(value = "d_up_detail", allEntries = true)
	public boolean delete(String id) {
		return upDetailDao.delete(id);
	}

	@Override
	@CacheEvict(value = "d_up_detail", allEntries = true)
	public boolean update(Map map) {
		return upDetailDao.update(map);

	}

	@Override
	@Cacheable(value = "d_up_detail",  key ="'queryById'+#id")
	public Map queryById(String id) {
		return upDetailDao.queryById(id);
	}
	

	@Override
	public Pages queryForList(Pages pages) {
		
		pages.setTotal(upDetailDao.queryForListCnt(pages.getQueryCondition()));
		List list=upDetailDao.queryForListByPage(pages.getAllQueryCond(), pages.getPageStart(), pages.getPageSize());
		pages.setDataList(list);
		return pages;
	}	
}
